/**
 * Created by IntelliJ IDEA.
 * User: ljy
 * Date: 2021/6/18
 * Time: 15:06
 */
package com.cowcode.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

public class Util {
    public static String CodeZiMu() {
        Random ran = new Random();
        String code = "";
        for(int i = 0 ; i < 6;i++) {
            if(0==ran.nextInt(2)) {//如果随机数0~1，如果是0就给数字，不是就给大写英文字母
                code+=ran.nextInt(10);
            } else {
                code+=(char)(ran.nextInt(26)+65);
            }
        }
        return code;
    }
    public static String codeNum(){
        Random ran = new Random();
        String code = "";
        for(int i = 0 ; i < 6;i++) {
            code+=ran.nextInt(10);
        }
        return code;
    }
    public static String crawler(String address) throws IOException {//爬虫方法
        InputStream in = null;
        InputStreamReader reader = null;
        URLConnection con = null;
        BufferedReader br = null;
        String result = "";
        try {
            URL url = new URL(address);//地址
            con = url.openConnection();
            in = con.getInputStream();
            reader = new InputStreamReader(in,"utf-8");
            br = new BufferedReader(reader);
            String s1 = "";
            while((s1=br.readLine())!=null) {
                result+=s1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(reader!=null) reader.close();
            if(in!=null) in.close();
        }
        return result;
    }
}
